package com.kimho.truck.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kimho.core.PageInfo;
import com.kimho.menu.domain.Menu;
import com.kimho.menu.service.MenuService;
import com.kimho.truck.domain.Truck;
import com.kimho.truck.service.TruckService;
import com.kimho.util.Util;
@Controller
@RequestMapping("/truck")
public class TruckController {
	@Autowired
	private TruckService truckService;
	
	/**
	 * 获取汽车列表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/items")
	public @ResponseBody Map<String,Object> getTruckList(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> responseMap = new HashMap<String,Object>();
		PageInfo pi = new PageInfo(request);
		List<Truck> trucklist = truckService.getTruckList(pi);
		responseMap.put("totalCount", truckService.getTotal());
		responseMap.put("rows", trucklist);
		return responseMap;
	}

	/**
	 * 保存用户信息（新增用户or编辑用户）
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> saveOrUpdate(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> responseMap = new HashMap<String,Object>();
		DateFormat format= new SimpleDateFormat("yyyy-MM-dd");
		String id = request.getParameter("id");
		String truck_id = request.getParameter("truck_id");
		String truck_nbr = request.getParameter("truck_nbr");
		String truck_type = request.getParameter("truck_type");
		String truck_run_lic = request.getParameter("truck_run_lic");
		String truck_load = request.getParameter("truck_load");
		String truck_long = request.getParameter("truck_long");
		String truck_hight = request.getParameter("truck_hight");
		String truck_width = request.getParameter("truck_width");
		String truck_peo = request.getParameter("truck_peo");
		String truck_con = request.getParameter("truck_con");
		String truck_fix_rec = request.getParameter("truck_fix_rec");
		String truck_owner_id = request.getParameter("truck_owner_id");
		String owner_id = request.getParameter("ownerList");
		
		
		try {
			//编辑用户信息
			if(!"".equals(id)) {
				Map<String,Object> param = new HashMap<String,Object>();
				param.put("id", id);
				Truck truck = truckService.getTruck(param);				
				truckService.updateTruck(truck, param);
				responseMap.put("success", "true");
				responseMap.put("info", "编辑成功！");
			}
			//新增用户信息
			else {
				Truck truck = new Truck();
//				Long newId = userService.getId();
				String newid = Util.getUUID();
				truck.setTruck_id(newid);
				truck.setTruck_nbr(truck_nbr);
				truck.setTruck_run_lic(truck_run_lic);
				truck.setTruck_fix_rec(truck_fix_rec);
				truck.setTruck_con(truck_con);
				truck.setTruck_load(Double.parseDouble(truck_load));
				truck.setTruck_type(truck_type);
				truckService.insertTurck(truck);
				responseMap.put("method", "Create");
				responseMap.put("success", "true");
				responseMap.put("info", "新增成功！");
			}
			return responseMap;
		}catch(Exception e) {
			e.printStackTrace();
			responseMap.put("info", e.getClass()+":"+e.getMessage());
			return responseMap;
		}
	}
	/**
	 * 删除用户
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/remove",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> remove(HttpServletRequest request,   
            HttpServletResponse response){
		Map<String,Object> responseMap = new HashMap<String,Object>();
		String ids = request.getParameter("ids");
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("ids", ids);
		try {
			truckService.deletTuck(param);
			responseMap.put("success", "true");
			responseMap.put("info", "删除成功！");
			return responseMap;
		}catch(Exception e) {
			e.printStackTrace();
			responseMap.put("info", e.getClass()+":"+e.getMessage());
			return responseMap;
		}
	}
	
}
